
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>

#include <qapplication.h>
#include <qlayout.h>
#include <qwt_plot.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_curve.h>
#include <qwt_legend.h>
#include <qwt_series_data.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_magnifier.h>
#include <qwt_text.h>
#include <qwt_math.h>
#include <math.h>

#include "itf_graph_xy.h"

#include "mex.h"

Plot::Plot(QWidget *parent)
{
    qwtplot = new QwtPlot(parent);
    
    // panning with the left mouse button
    QwtPlotPanner *pb = new QwtPlotPanner(qwtplot->canvas());
    
    // zoom in/out with the wheel
    QwtPlotMagnifier *zio = new QwtPlotMagnifier(qwtplot->canvas());
    
    qwtplot->setAutoFillBackground( true );
    qwtplot->setPalette( QPalette( QColor( 255, 255, 255 ) ) );

    qwtplot->setTitle("CDF of state events");
    qwtplot->insertLegend(new QwtLegend(), QwtPlot::RightLegend);

    // axes 
    qwtplot->setAxisTitle(qwtplot->xBottom, "Time (s)");
    qwtplot->setAxisScale(qwtplot->xBottom, 0.0, 10.0);

    qwtplot->setAxisTitle(qwtplot->yLeft, "CDF");
    qwtplot->setAxisScale(qwtplot->yLeft, 0.0, 1.0);
    
    // canvas
    qwtplot->canvas()->setLineWidth(1);
    qwtplot->canvas()->setFrameStyle(QFrame::Box | QFrame::Plain);
    qwtplot->canvas()->setBorderRadius(15);

    QPalette canvasPalette(Qt::white);
    canvasPalette.setColor(QPalette::Foreground, QColor(133, 190, 232));
    qwtplot->canvas()->setPalette(canvasPalette);
    
    //populate();
}

QwtPlot* Plot::getQwtPlot() {
    return qwtplot;
}

void Plot::setValue(double *clkarray, unsigned int clk_size, unsigned int eventid, unsigned int stateid) {
	mexPrintf("SET..... %d!!\n", clk_size);
    
    
    QwtPlotCurve *cSin = new QwtPlotCurve(QString().setNum(eventid));
    cSin->setPen(QPen(QColor(rand()%255, rand()%255, rand()%255)));
    cSin->attach(qwtplot);
    
    cSin->setData(new QwtPointSeriesData(*ecdf(clkarray, clk_size)));
    
    qwtplot->replot();
}

void Plot::populate()
{
    // Insert new curves
    QwtPlotCurve *cSin = new QwtPlotCurve("y = sin(x)");
    cSin->setRenderHint(QwtPlotItem::RenderAntialiased);
    cSin->setLegendAttribute(QwtPlotCurve::LegendShowLine, true);
    cSin->setPen(QPen(Qt::red));
    cSin->attach(qwtplot);
    
    
    QVector<QPointF> integerVector(4);
    integerVector[0]=QPointF(1,0.2);
    integerVector[1]=QPointF(2,0.6);
    integerVector[2]=QPointF(3,0.7);
    integerVector[3]=QPointF(4,0.8);
    
    cSin->setData(new QwtPointSeriesData(integerVector));
    
    //  ...a horizontal line at y = 0...
    QwtPlotMarker *mY = new QwtPlotMarker();
    mY->setLabel(QString::fromLatin1("y = 0"));
    mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
    mY->setLineStyle(QwtPlotMarker::HLine);
    mY->setYValue(0.0);
    mY->attach(qwtplot);
    
    //cSin->setData(new FunctionData(::sin));
}

QVector<QPointF> * Plot::ecdf(double *clkarray, unsigned int clk_size)
{
    unsigned int i;
    mxArray *input[1], *out[2];
    double *clk, *x, *y;
    double size_v;
    
    QVector<QPointF> *ecdfVector;
    
    input[0] =  mxCreateDoubleMatrix(1, clk_size, mxCOMPLEX);
    
    clk = (double *) mxGetData(input[0]);
    
    //Copy clkarray to clk
    memcpy(clk, clkarray, clk_size*sizeof(double));
    
    /* Call the Matlab funtion '[x y]=ecdf(randn(1,100))' */
    mexCallMATLAB(2, out, 1, input, "ecdf");
    
    x = (double *) mxGetData(out[0]);
    y = (double *) mxGetData(out[1]);
    
    size_v = mxGetM(out[0]);
    assert(size_v == mxGetM(out[1]));
    
    ecdfVector = new QVector<QPointF>(size_v);
    
    for(i=0; i<size_v; i++) {
        ecdfVector->push_back(QPointF(y[i],x[i]));
    }
    
    return ecdfVector;
    
}

